package Frame.shopSet;

import FCRendering.FCRMaster;
import FCRendering.sw.FCRValues;
import Frame.entity.UnifyShopSayUIEntity;
import VWorldGameStudio.*;
import lombok.Setter;
import Function.FCItemForTankHelper;
import Function.FCMutilPlayCache;

import java.util.Vector;

import static FCRendering.FCRMaster.*;

/**
 * 补满服务
 */
public class FunctionBuManSop extends AShopFuntion{
    private Vector<FixTank> needFillServiceNames = new Vector<>();

    private int showFristIndex = 0;
    @Setter
    private int endI = 0;
    private int needAllMoney = 0;
    public FunctionBuManSop(){
        buildShopWindow();
    }
    private int selectIndex = -1;
    public void buildShopWindow(){
        needFillServiceNames.clear();
        showFristIndex = 0;
        endI = 0;
        needAllMoney = 0;
        selectIndex = -1;
        FCTank tank = tankLib.getTankOfID(player1.getPlayertank1(), player1.getPlayertankHostId());
        if (tank != null) {
            checkAndAdd(tank);
        }

        //这里帮了别人的坦克补了状态
        for (FCRPlayerLink link : FCMutilPlayCache.getTeamMembers()) {
            if(!link.curPlayerHostUID.equals(gameSave.HOST_UID)) {
                continue;
            }
            FCPlayerInfo p = FCMutilPlayCache.findPlayerByName(link.curPlayerHostUID, link.curPlayerName);
            if (p != null) {
                tank = tankLib.getTankOfID(p.getPlayertank1(), p.getPlayertankHostId());
                if (tank != null) {
                    checkAndAdd(tank);
                }
            }
        }
        //还有牵引的
        for (FCPropulsion fc : fcv.getPropulsions()) {
            if(fc.getPropulsionHostId().equals(gameSave.HOST_UID) && fc.getPropulsionType().equals("tank")){
                checkAndAdd((FCTank) fc);
            }
        }

        setEndI(Math.min(needFillServiceNames.size(), showFristIndex + 6));
        resetUI();
    }

    private void checkAndAdd(FCTank tank) {
        FCItem item = FCRMaster.itemLib.getItemOfID(tank.getGunID());
        if (item != null) {
            if(item.getCapacity() > 0){//弹容大于0
                if(item.getCurrentCapacityVal() < item.getCapacity()){
                    //并且当前弹数小于弹容
                    int needNums = (int)(item.getCapacity() - item.getCurrentCapacityVal());
                    needAllMoney += needNums*item.getFillServicePrice();
                    needFillServiceNames.add(new FixTank(tank.getGunID(), tank, needNums*item.getFillServicePrice(),
                            langLib.getNameOfId(tank.getName())+"-主炮-弹数 "+item.getCurrentCapacityVal()+"/"+(int)item.getCapacity()));
                }
            }
        }
        item = FCRMaster.itemLib.getItemOfID(tank.getSEID());
        if (item != null) {
            if(item.getCapacity() > 0){//弹容大于0
                if(item.getCurrentCapacityVal() < item.getCapacity()){
                    //并且当前弹数小于弹容
                    int needNums = (int)(item.getCapacity() - item.getCurrentCapacityVal());
                    needAllMoney += needNums*item.getFillServicePrice();
                    needFillServiceNames.add(new FixTank(tank.getSEID(), tank, needNums*item.getFillServicePrice(),
                            langLib.getNameOfId(tank.getName())+"-SE-弹数 "+item.getCurrentCapacityVal()+"/"+(int)item.getCapacity()));
                }
            }
        }
        /**
         * 特俗弹药暂时不自动补满，要单独购买
         */

        if(tank.getMaxDefense() > tank.getDefense()){
            //就是还能继续补装甲片
            int needNums = tank.getMaxDefense() - tank.getDefense();
            item = FCRMaster.itemLib.getItemOfID("PG1");
            if(item != null){
                needAllMoney += needNums*item.getFillServicePrice();
                needFillServiceNames.add(new FixTank("PG1", tank, needNums*item.getFillServicePrice(),
                        langLib.getNameOfId(tank.getName())+"-装甲片 "+tank.getDefense()+"/"+tank.getMaxDefense()));
            }
        }
    }

    private void resetUI(){
        FCRValues.resetUnifySayUIEntity();
        FCRValues.leftWindowUI[0].leftText = "全部补满";
        FCRValues.leftWindowUI[0].rightText = needAllMoney+"G";
        int m = 1;
        for (int i = showFristIndex; i < endI; i++) {
            FixTank fixTank = needFillServiceNames.get(i);
            FCRValues.leftWindowUI[m].leftText = fixTank.text;
            m++;
        }
        FCRValues.rightWindowUI[1].leftText = "英雄再强，没弹药也是虫.";
        FCRValues.rightWindowUI[3].rightText = "上一页";
        FCRValues.rightWindowUI[4].rightText = "下一页";
        FCRValues.rightWindowUI[5].rightText = "部分补满";
        FCRValues.rightWindowUI[6].rightText = "退出";
    }

    @Override
    public void clickLeftWindow(int i, UnifyShopSayUIEntity entity) {
        if(i == 0){
            //全部修理
            if(player1.getMoney() >= needAllMoney){
                if(needFillServiceNames.size() == 0){
                    FCRValues.rightWindowUI[1].leftText = "你的战车不需要补满服务了";
                }else {
                    for (FixTank f : needFillServiceNames) {
                        fixTankDeal(f);
                    }
                    needFillServiceNames.clear();
                    needAllMoney = 0;
                    setEndI(Math.min(needFillServiceNames.size(), showFristIndex + 6));
                    resetUI();
                    FCRValues.rightWindowUI[1].leftText = "全部补满完成，请继续";
                }
            }else {
                FCRValues.rightWindowUI[1].leftText = "你的金币好像不太够喔！";
            }
        }else {
            //单独
            if(entity.leftText.length() > 1) {
                selectIndex = showFristIndex + i - 1;
                FixTank fixTank = needFillServiceNames.get(selectIndex);
                FCItem item = itemLib.getItemOfID(fixTank.itemName);
                if(item != null) {
                    if ("PG1".equals(fixTank.itemName)) {
                        //装甲片
                        FCRValues.rightWindowUI[2].leftText = "补10SP "+item.getFillServicePrice()*10+"G";
                    } else {
                        FCRValues.rightWindowUI[2].leftText = "补单发子弹 "+item.getFillServicePrice()+"G";
                    }
                }
            }
        }
    }

    private boolean fixTankDeal(FixTank fixTank){
        if(player1.getMoney() >= fixTank.needFixMoney){
            FCItem item = itemLib.getItemOfID(fixTank.itemName);
            if(item != null){
                if("PG1".equals(fixTank.itemName)){
                    //装甲片
                    fixTank.tank.setDefense(fixTank.tank.getMaxDefense());
                }else {
                    FCItemForTankHelper.fullDanyao(item);
                }
                player1.setMoney(player1.getMoney() - fixTank.needFixMoney);
                needAllMoney -= fixTank.needFixMoney;
                fixTank.tank.reInitShuxing();
                return true;
            }
        }else {
            FCRValues.rightWindowUI[1].leftText = "你的金币好像不太够喔！";
        }
        return false;
    }

    @Override
    public void clickRightWindow(int i, UnifyShopSayUIEntity entity) {
        switch (i) {
            case 2:
                if(selectIndex >= 0) {
                    FixTank fixTank = needFillServiceNames.get(selectIndex);
                    FCItem item = itemLib.getItemOfID(fixTank.itemName);
                    int needM = 0;
                    if (item != null) {
                        if ("PG1".equals(fixTank.itemName)) {
                            //装甲片
                            needM = item.getFillServicePrice()*10;
                        } else {
                            needM = item.getFillServicePrice();
                        }
                        if(player1.getMoney() >= needM){
                            if ("PG1".equals(fixTank.itemName)) {
                                //装甲片
                                fixTank.tank.setDefense(fixTank.tank.getDefense()+10);
                            } else {
                                item.setCurrentCapacityVal(item.getCurrentCapacityVal()+1);
                                if(item.getCurrentCapacityVal() > item.getCapacity()){
                                    item.setCurrentCapacityVal((int)item.getCapacity());
                                }
                            }
                            player1.setMoney(player1.getMoney() - needM);
                            needAllMoney -= needM;
                            fixTank.tank.reInitShuxing();

                            buildShopWindow();
                        }else {
                            FCRValues.rightWindowUI[1].leftText = "你的金币好像不太够喔！";
                        }
                    }
                }
                break;
            case 3:
                if (showFristIndex >= 6) {
                    showFristIndex -= 6;
                    setEndI(Math.min(needFillServiceNames.size(), showFristIndex + 6));
                    resetUI();
                }
                break;
            case 4:
                if (endI < needFillServiceNames.size()) {
                    showFristIndex += 6;
                    setEndI(Math.min(needFillServiceNames.size(), showFristIndex + 6));
                    resetUI();
                }
                break;
            case 5:
                if(selectIndex >= 0){
                    FixTank fixTank = needFillServiceNames.get(selectIndex);
                    if(fixTankDeal(fixTank)) {
                        needFillServiceNames.remove(fixTank);
                        setEndI(Math.min(needFillServiceNames.size(), showFristIndex + 6));
                        resetUI();
                        FCRValues.rightWindowUI[1].leftText = "部分补满完成,请继续";
                    }
                }
                break;
            case 6:
                fcv.setOpenSayShop(false);
                FCRMaster.fcv.setOpenSay(false);
                FCRMaster.fcv.setSayNpcName(null);
                FCRMaster.fcv.setSayNpcTextNum(0);
                break;
        }
    }

}
